package MyThreadPool;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 测试类
 */
public class Test {

	//线程编号
	private static final AtomicInteger threadNum = new AtomicInteger(1);

	public static void main(String[] args) throws InterruptedException {

		MyThreadPool threadPool = new MyThreadPool(4);
		print("线程池已开启...");
		print("线程池最大线程容量为：" + threadPool.getMaxSize());
		for(int i=0;i<10;i++) {
			threadPool.execute(() -> {
				System.out.println("Thread-" + threadNum.getAndIncrement() + "正在运行...");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		//提交空任务，提示提交任务不能为空
		threadPool.execute(null);
		print("当前任务队列大小为：" + threadPool.getQueueSize());
		print("当前活跃线程数量为：" + threadPool.getActiveCount());
		Thread.sleep(5000);
		threadPool.shutdown();
		print("----------------------------");
		print("线程池关闭状态为：" + threadPool.isShutdown());
		print("线程池已关闭...");
		print("----------------------------");
		print("工作线程个数为：" + threadPool.getActiveCount());
		print("当前任务队列大小为：" + threadPool.getQueueSize());
		print("关闭后，尝试提交任务：");
		//关闭后提交任务，抛异常
		threadPool.execute(() -> {
			System.out.println("Thread-" + threadNum.getAndIncrement() + "正在运行...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
	}
	public static void print(String str) {
		System.out.println(str);
	}

}